package com.example.controller;

import com.example.entity.PageBean;
import com.example.entity.Result;
import com.example.entity.vo.MonthData;
import com.example.entity.vo.UserStatistic;
import com.example.entity.vo.WeekResponse;
import com.example.service.AdminService;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/user")
    public Result<PageBean> pageUser(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 String username, String email){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer permission = (Integer) map.get("permission");
        if (permission != 0){
            return Result.error("用户没有权限");
        }
        PageBean pageBean = adminService.pageUser(page, pageSize, username, email);
        return Result.success(pageBean);
    }

    @DeleteMapping("/user")
    public Result<Object> deleteUser(@RequestParam Integer id){
        adminService.deleteUser(id);
        return Result.success();
    }

    @GetMapping("/display")
    public Result<UserStatistic> receiveUserStatistic(){
        UserStatistic userStatistic = adminService.receiveUserStatistic();
        return Result.success(userStatistic);
    }

    @GetMapping("/month")
    public Result<MonthData> getMonthsData(){
        MonthData monthData = adminService.getMonthsData();
        return Result.success(monthData);
    }

    @GetMapping("/week")
    public Result<WeekResponse> getWeekData(){
        WeekResponse weekData = adminService.getWeekData();
        return Result.success(weekData);
    }

}
